/**
 * 碰撞检测像素形状
 */
var	HitableShape		= function (matrix) {

	this.setMatrix(matrix);
	this._ID	= ++ HitableShape._IDSeed;
};

HitableShape._IDSeed		= 0;
HitableShape._ready			= false;
HitableShape.eventNameList	= [
	'hitableShape.hit',
	'hitableShape.adjacent'
];

HitableShape.isOwnEventName	= function (eventName) {
	
	for (var offset = 0;offset < HitableShape.eventNameList.length;offset ++) {
		
		if (HitableShape.eventNameList[offset] == eventName) {
			
			return	true;
		}
	}
	
	return	false;
}

/**
 * 注册事件名
 */
HitableShape.initailize	= function () {
	
	if (HitableShape._ready) {
		
		return	;
	}

	for (var offset = 0;offset < HitableShape.eventNameList.length;offset ++) {
		
		LBEvent.registName(HitableShape.eventNameList[offset]);
	}
	
	HitableShape._ready	= true;
}

LBUtility.getInstance().registSelector('lib/core/EventDispatcher/LBEventDispatcher.js');
LBUtility.getInstance().registSelector('lib/core/EventDispatcher/LBEventListener.js');
LBUtility.getInstance().registSelector('lib/core/EventDispatcher/LBEvent.js');

HitableShape.prototype.setMatrix	= function (matrix) {
	
	this._matrix	= matrix;
}

HitableShape.prototype.getMatrix	= function () {
	
	return	this._matrix;
}

HitableShape.prototype.setPosition	= function (x, y) {

	this._x	= x;
	this._y	= y;
}

HitableShape.prototype.getPosition	= function () {

	return	{
		"x"	: this._x,
		"y"	: this._y
	}
}

HitableShape.prototype.getID		= function () {
	
	return	this._ID;
}

HitableShape.prototype.addEventListener	= function (eventName, listener, priority) {
	
	if (!HitableShape.isOwnEventName(eventName)) {
		
		throw	"ownner of this event name '" + eventName + "' not HitableShape";
	}

    LBEventDispatcher
        .getInstance()
        .addEventListener(
            new LBEvent({
                "name"      : eventName,
                "target"    : this,
                "id"        : this._ID
            }),
            listener,
            priority
        );
}

HitableShape.prototype.removeEventListener	= function (eventName, listener, priority) {
	
	if (!Frame.isOwnEventName(eventName)) {
		
		throw	"ownner of this event name '" + eventName + "' not HitableShape";
	}

    LBEventDispatcher
        .getInstance()
        .removeEventListener(
            new LBEvent({
                "name"      : eventName,
                "target"    : this,
                "id"        : this._id
            }),
            listener,
            priority
        );
}

HitableShape.prototype.debugByRender	= function (render, pointRander, scale) {

	this._debugRender	= render;
	this._pointRander	= pointRander;
	this._debugScale	= scale;
}

HitableShape.prototype.doDebug	= function () {
	
	if (this._debugRender && this._matrix) {

		var	posX	= this._x	? this._x	: 0;
		var	posY	= this._y	? this._y	: 0;
		
		for (var offsetRow = 0;offsetRow < this._matrix.length;offsetRow ++) {
			
			for (var offsetColumn = 0;offsetColumn < this._matrix[offsetRow].length;offsetColumn ++) {
				
				if (this._matrix[offsetRow][offsetColumn]) {

					this._pointRander.setPosition((posX + offsetRow) * this._debugScale, (posY + offsetColumn) * this._debugScale, 'absolute');
					this._pointRander.setContext(this._debugRender._context);
					this._pointRander.draw();
				}
			}
		}
	}
}
